package com.kk.permission.utils;

import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;
import android.os.Handler;

public class PrefUtilImpl {
    private static final long INSERT_MIN_INTERVAL_NANOS = 1000000000;
    private static final String QUERY_EXIST_SELECTION = "query_type=? AND key=?";
    private static final String QUERY_VALUE_SELECTION = "query_type=? AND key=? AND type=? AND default=?";
    private static final String TAG = "PrefUtil";
    private static final Object sDeletedValue = new Object();
    private static long sLastCheckTime;
    private static ContentValues sPendingValues = new ContentValues();
    private static Uri sURI;
    private Object URI;

    PrefUtilImpl() {
    }

    static {
        getURI();
    }

    private static boolean shouldInsertNow() {
        return System.nanoTime() - sLastCheckTime >= INSERT_MIN_INTERVAL_NANOS;
    }

    public static void insertPendingValues() {
        if (sPendingValues.size() != 0 && shouldInsertNow()) {
            sLastCheckTime = System.nanoTime();
            new Handler().post(new Runnable() {
                @Override
                public void run() {
                    if (PrefUtilImpl.sPendingValues.size() > 0) {
                        PrefUtilImpl.doInsertPendingValues();
                    }
                }
            });
//            BackgroundExecutor.execute(new Runnable() {
//                public void run() {
//                    if (PrefUtilImpl.sPendingValues.size() > 0) {
//                        PrefUtilImpl.doInsertPendingValues();
//                    }
//                }
//            }, ThreadType.IO);
        }
    }

    private static void doInsertPendingValues() {
        ContentValues contentValues;
        synchronized (sPendingValues) {
            contentValues = new ContentValues(sPendingValues);
            sPendingValues.clear();
        }
        try {
            BaseUtil.getAppContext().getContentResolver().insert(getURI(), contentValues);
            if (BaseUtil.getAdapter().isStrictDebugMode()) {
            }
        } catch (Throwable e) {
        }
    }

    public static int getKeyInt(String str, int i) {
        if (sPendingValues.size() <= 0 || !sPendingValues.containsKey(str)) {
            try {
                Cursor query = BaseUtil.getAppContext().getContentResolver().query(getURI(), null, QUERY_VALUE_SELECTION, new String[]{"value", str, "integer", String.valueOf(i)}, null);
                if (query == null) {
                    return i;
                }
                if (query.moveToFirst()) {
                    i = query.getInt(0);
                }
                if (!(query == null || query.isClosed())) {
                    query.close();
                }
                return i;
            } catch (Throwable e) {
            }
        } else {
            Object obj = sPendingValues.get(str);
            return obj instanceof Integer ? ((Integer) obj).intValue() : i;
        }
        return i;
    }

    public static int getKeyIntRes(String str, int i) {
        return getKeyInt(str, BaseUtil.getAppContext().getResources().getInteger(i));
    }

    public static boolean getKeyBoolean(String str, boolean z) {
        if (sPendingValues.size() <= 0 || !sPendingValues.containsKey(str)) {
            try {
                Cursor query = BaseUtil.getAppContext().getContentResolver().query(getURI(), null, QUERY_VALUE_SELECTION, new String[]{"value", str, "boolean", String.valueOf(z)}, null);
                if (query == null) {
                    return z;
                }
                if (query.moveToFirst()) {
                    z = query.getInt(0) > 0;
                }
                if (!(query == null || query.isClosed())) {
                    query.close();
                }
                return z;
            } catch (Throwable e) {
            }
        } else {
            Object obj = sPendingValues.get(str);
            return obj instanceof Boolean ? ((Boolean) obj).booleanValue() : z;
        }
        return z;
    }

    public static boolean getKeyBooleanRes(String str, int i) {
        return getKeyBoolean(str, BaseUtil.getAppContext().getResources().getBoolean(i));
    }

    public static String getKeyString(String str, String str2) {
        if (sPendingValues.size() <= 0 || !sPendingValues.containsKey(str)) {
            try {
                Cursor query = BaseUtil.getAppContext().getContentResolver().query(getURI(), null, QUERY_VALUE_SELECTION, new String[]{"value", str, "string", str2}, null);
                if (query == null) {
                    return str2;
                }
                if (query.moveToFirst()) {
                    str2 = query.getString(0);
                }
                if (!(query == null || query.isClosed())) {
                    query.close();
                }
                return str2;
            } catch (Throwable e) {
            }
        } else {
            Object obj = sPendingValues.get(str);
            return obj instanceof String ? (String) obj : str2;
        }
        return str;
    }

    public static String getKeyStringRes(String str, int i) {
        return getKeyString(str, BaseUtil.getAppContext().getResources().getString(i));
    }

    public static long getKeyLong(String str, long j) {
        if (sPendingValues.size() <= 0 || !sPendingValues.containsKey(str)) {
            try {
                Cursor query = BaseUtil.getAppContext().getContentResolver().query(getURI(), null, QUERY_VALUE_SELECTION, new String[]{"value", str, "long", String.valueOf(j)}, null);
                if (query == null) {
                    return j;
                }
                if (query.moveToFirst()) {
                    j = query.getLong(0);
                }
                if (!(query == null || query.isClosed())) {
                    query.close();
                }
                return j;
            } catch (Throwable e) {
            }
        } else {
            Object obj = sPendingValues.get(str);
            return obj instanceof Long ? ((Long) obj).longValue() : j;
        }
        return j;
    }

    public static float getKeyFloat(String str, float f) {
        if (sPendingValues.size() <= 0 || !sPendingValues.containsKey(str)) {
            try {
                Cursor query = BaseUtil.getAppContext().getContentResolver().query(getURI(), null, QUERY_VALUE_SELECTION, new String[]{"value", str, "float", String.valueOf(f)}, null);
                if (query == null) {
                    return f;
                }
                if (query.moveToFirst()) {
                    f = query.getFloat(0);
                }
                if (!(query == null || query.isClosed())) {
                    query.close();
                }
                return f;
            } catch (Throwable e) {
            }
        } else {
            Object obj = sPendingValues.get(str);
            return obj instanceof Float ? ((Float) obj).floatValue() : f;
        }
        return f;
    }

    public static void setKey(String str, int i) {
        synchronized (sPendingValues) {
            sPendingValues.put(str, Integer.valueOf(i));
        }
        insertPendingValues();
    }

    public static void setKey(String str, boolean z) {
        synchronized (sPendingValues) {
            sPendingValues.put(str, Boolean.valueOf(z));
        }
        insertPendingValues();
    }

    public static void setKey(String str, String str2) {
        synchronized (sPendingValues) {
            sPendingValues.put(str, str2);
        }
        insertPendingValues();
    }

    public static void setKey(String str, long j) {
        synchronized (sPendingValues) {
            sPendingValues.put(str, Long.valueOf(j));
        }
        insertPendingValues();
    }

    public static void setKey(String str, float f) {
        synchronized (sPendingValues) {
            sPendingValues.put(str, Float.valueOf(f));
        }
        insertPendingValues();
    }

    public static void deleteKey(String str) {
        synchronized (sPendingValues) {
            sPendingValues.putNull(str);
        }
        insertPendingValues();
    }

    public static boolean containsKey(String str) {
        boolean z = false;
        if (sPendingValues.size() <= 0 || !sPendingValues.containsKey(str)) {
            try {
                Cursor query = BaseUtil.getAppContext().getContentResolver().query(getURI(), null, QUERY_EXIST_SELECTION, new String[]{"exist", str}, null);
                if (query != null && query.getCount() > 0) {
                    z = true;
                }
                if (!(query == null || query.isClosed())) {
                    query.close();
                }
            } catch (Throwable e) {
            }
            return z;
        }
        if (sPendingValues.get(str) != null) {
            z = true;
        }
        return z;
    }

    private static Uri getURI() {
        if (sURI == null) {
            String packageName = BaseUtil.getAppContext().getPackageName();
            sURI = Uri.parse(String.format("content://%s%s", new Object[]{packageName, ".base.pref.PreferenceProvider"}));
        }
        return sURI;
    }
}